<?php
namespace app\dboard\controller;
use think\captcha;
use think\facade\Cookie;
use think\facade\Session;
use think\facade\Url;
use think\Db;
use think\facade\Cache;
use services\ImmService;
use services\OssService;
class Publics extends Base
{
    public function initialize() {
        parent::initialize();
/*
        $returnUrl=Url::build('dboard/login/sign');
        if(!isset($this->AdminAccount['AdminID']) || intval($this->AdminAccount['AdminID'])<=0){
            if($this->request->isAjax()){
                return $this->print_result(['Code'=>9999,'Msg'=>'请登录']);
            }
            return $this->redirect($returnUrl);
        }
*/
    }
    public function gettree()
    {
        $sid = $this->request->param('sid');
        $tid = $this->request->param('tid');
        $type = $this->request->param('type');
        if (!$sid && !$tid) {
            $this->res_data['Code']=1;
            $this->res_data['Data']=[];
            return $this->print_result($this->res_data);
        }
        if ($sid && $tid) {
            $this->res_data['Code']=1;
            $this->res_data['Data']=[];
            return $this->print_result($this->res_data);
        }
        if (!$type) {
            $type=1;
        }
        if ($sid) {
            $where = ' and ca.sid='.$sid;
        } else {
            $where = ' and ca.teacher_id='.$tid;
        }
        $courseware_course = Db::name('courseware_course')->alias('cc')->leftJoin('courseware_authorize ca','cc.id=ca.course_id'.$where.' and ca.type='.$type.' and ca.status=1')->field('cc.id,cc.name as text,cc.pid as parentId,ca.status as selected')->where(['cc.status'=>1,'cc.pid'=>0])->order('cc.sort ASC')->select();
        if ($courseware_course) {
            foreach ($courseware_course as $key => $value) {
                $nodes1 = Db::name('courseware_course')->alias('cc')->leftJoin('courseware_authorize ca','cc.id=ca.course_id'.$where.' and ca.type='.$type.' and ca.status=1')->field('cc.id,cc.name as text,cc.pid as parentId,ca.status as selected')->where(['cc.status'=>1,'cc.pid'=>$value['id']])->order('cc.sort ASC')->select();
                if ($nodes1) {
                    foreach ($nodes1 as $k => $v) {
                        $nodes2 = Db::name('courseware_course')->alias('cc')->leftJoin('courseware_authorize ca','cc.id=ca.course_id'.$where.' and ca.type='.$type.' and ca.status=1')->field('cc.id,cc.name as text,cc.pid as parentId,ca.status as selected')->where(['cc.status'=>1,'cc.pid'=>$v['id']])->order('cc.sort ASC')->select();
                        if ($nodes2) {
                            foreach ($nodes2 as $kk => $vv) {
                                $nodes2[$kk]['tags'] = [$vv['id']];
                                $nodes2[$kk]['state']['selected'] = $vv['selected']?1:0;
                            }
                        }
                        $tags2 = [$v['id']];
                        $nodes1[$k]['nodes'] = $nodes2;
                        $nodes1[$k]['tags'] = $tags2;
                        $nodes1[$k]['state']['selected'] = $v['selected']?1:0;
                    }
                }
                $tags1 = [$value['id']];
                $courseware_course[$key]['nodes'] = $nodes1;
                $courseware_course[$key]['tags'] = $tags1;
                $courseware_course[$key]['state']['selected'] = $value['selected']?1:0;
            }
        }
        $this->res_data['Msg']='OK';
        $this->res_data['Code']=0;
        $this->res_data['Data']=$courseware_course;
        return $this->print_result($this->res_data);
    }
    public function gettree2()
    {
        $sid = $this->request->param('sid');
        $tid = $this->request->param('tid');
        $type = $this->request->param('type');
        if (!$sid && !$tid) {
            $this->res_data['Code']=1;
            $this->res_data['Data']=[];
            return $this->print_result($this->res_data);
        }
        if (!$type) {
            $type=1;
        }
        if ($sid) {
            $where = ' and ca.sid='.$sid;
        } else {
            $where = ' and ca.teacher_id='.$tid;
        }
        $courseware_course = Db::name('courseware_authorize')->alias('ca')->leftJoin('courseware_course cc','cc.id=ca.course_id')->leftJoin('courseware_authorize ca2','cc.id=ca2.course_id and ca2.teacher_id='.$tid.' and ca2.status=1 and ca2.type='.$type)->field('cc.id,cc.name as text,cc.pid as parentId,ca2.status as selected')->where(['ca.status'=>1,'ca.sid'=>$sid,'cc.pid'=>0,'cc.status'=>1])->order('cc.sort ASC')->select();
        if ($courseware_course) {
            foreach ($courseware_course as $key => $value) {
                $nodes1 = Db::name('courseware_authorize')->alias('ca')->leftJoin('courseware_course cc','cc.id=ca.course_id')->leftJoin('courseware_authorize ca2','cc.id=ca2.course_id and ca2.teacher_id='.$tid.' and ca2.status=1 and ca2.type='.$type)->field('cc.id,cc.name as text,cc.pid as parentId,ca2.status as selected')->where(['ca.status'=>1,'ca.sid'=>$sid,'cc.pid'=>$value['id'],'cc.status'=>1])->order('cc.sort ASC')->select();
                if ($nodes1) {
                    foreach ($nodes1 as $k => $v) {
                        $nodes2 = Db::name('courseware_authorize')->alias('ca')->leftJoin('courseware_course cc','cc.id=ca.course_id')->leftJoin('courseware_authorize ca2','cc.id=ca2.course_id and ca2.teacher_id='.$tid.' and ca2.status=1 and ca2.type='.$type)->field('cc.id,cc.name as text,cc.pid as parentId,ca2.status as selected')->where(['ca.status'=>1,'ca.sid'=>$sid,'cc.pid'=>$v['id'],'cc.status'=>1])->order('cc.sort ASC')->select();
                        if ($nodes2) {
                            foreach ($nodes2 as $kk => $vv) {
                                $nodes2[$kk]['tags'] = [$vv['id']];
                                $nodes2[$kk]['state']['selected'] = $vv['selected']?1:0;
                            }
                        }
                        $tags2 = [$v['id']];
                        $nodes1[$k]['nodes'] = $nodes2;
                        $nodes1[$k]['tags'] = $tags2;
                        $nodes1[$k]['state']['selected'] = $v['selected']?1:0;
                    }
                }
                $tags1 = [$value['id']];
                $courseware_course[$key]['nodes'] = $nodes1;
                $courseware_course[$key]['tags'] = $tags1;
                $courseware_course[$key]['state']['selected'] = $value['selected']?1:0;
            }
        }
        $this->res_data['Msg']='OK';
        $this->res_data['Code']=0;
        $this->res_data['Data']=$courseware_course;
        return $this->print_result($this->res_data);
    }


    //object(AlibabaCloud\SDK\Imm\V20170906\Models\GetOfficePreviewURLResponse)#62 (4) { ["headers"]=> array(9) { ["Date"]=> array(1) { [0]=> string(29) "Tue, 18 May 2021 01:55:56 GMT" } ["Content-Type"]=> array(1) { [0]=> string(30) "application/json;charset=utf-8" } ["Content-Length"]=> array(1) { [0]=> string(3) "403" } ["Connection"]=> array(1) { [0]=> string(10) "keep-alive" } ["Access-Control-Allow-Origin"]=> array(1) { [0]=> string(1) "*" } ["Access-Control-Allow-Methods"]=> array(1) { [0]=> string(18) "POST, GET, OPTIONS" } ["Access-Control-Allow-Headers"]=> array(1) { [0]=> string(57) "X-Requested-With, X-Sequence, _aop_secret, _aop_signature" } ["Access-Control-Max-Age"]=> array(1) { [0]=> string(6) "172800" } ["x-acs-request-id"]=> array(1) { [0]=> string(36) "A8D80DEA-A4F0-403F-91ED-6D84CBFD7A4F" } } ["body"]=> object(AlibabaCloud\SDK\Imm\V20170906\Models\GetOfficePreviewURLResponseBody)#72 (8) { ["previewURL"]=> string(127) "https://office-cn-hangzhou.imm.aliyuncs.com/office/p/67afe0f3b5723bc88dbafaa0802f58f3b53a0afb?hidecmb=1&simple=1&_w_tokentype=1" ["refreshToken"]=> string(34) "9606316e782e4e078db943242051ea7bv2" ["requestId"]=> string(36) "A8D80DEA-A4F0-403F-91ED-6D84CBFD7A4F" ["accessToken"]=> string(34) "259b2a4427244332bf584beb1f022eb8v2" ["refreshTokenExpiredTime"]=> string(24) "2021-05-19T01:55:56.625Z" ["accessTokenExpiredTime"]=> string(24) "2021-05-18T02:25:56.625Z" ["_name":protected]=> array(6) { ["previewURL"]=> string(10) "PreviewURL" ["refreshToken"]=> string(12) "RefreshToken" ["requestId"]=> string(9) "RequestId" ["accessToken"]=> string(11) "AccessToken" ["refreshTokenExpiredTime"]=> string(23) "RefreshTokenExpiredTime" ["accessTokenExpiredTime"]=> string(22) "AccessTokenExpiredTime" } ["_required":protected]=> array(0) { } } ["_name":protected]=> array(2) { ["headers"]=> string(7) "headers" ["body"]=> string(4) "body" } ["_required":protected]=> array(0) { } }
    //array(6) { ["previewURL"]=> string(127) "https://office-cn-hangzhou.imm.aliyuncs.com/office/p/67afe0f3b5723bc88dbafaa0802f58f3b53a0afb?hidecmb=1&simple=1&_w_tokentype=1" ["RefreshToken"]=> string(34) "f23fb65b5a77471a859179aead92cfe4v2" ["RequestId"]=> string(36) "0F9C079B-8AA1-493F-97F0-6C1086F684BD" ["AccessToken"]=> string(34) "75cd59f3093247f1bb35fa581307eb78v2" ["RefreshTokenExpiredTime"]=> string(23) "2021-05-19T01:58:27.17Z" ["AccessTokenExpiredTime"]=> string(23) "2021-05-18T02:28:27.17Z" }
    public function getPreviewInfo()
    {
        // 文件名
        $id = $this->request->param('id');
        if (!$id) {
            $this->res_data['Code']=1;
            $this->res_data['Data']=[];
            $this->res_data['Msg']='fail';
            return $this->print_result($this->res_data);
        }
        $name = $id;
        if (strpos($id, '/')!==false) {
            $arrs = explode('/', $id);
            $name = end($arrs);
        }
        $arr = Cache::get($name);
        if (isset($arr['accessTokenExpiredTime']) && time()<strtotime($arr['accessTokenExpiredTime'])) {
            $this->res_data['Msg']='OK';
            $this->res_data['Code']=0;
            $this->res_data['Data']=$arr;
        }
        if (isset($arr['refreshTokenExpiredTime']) && time()<strtotime($arr['refreshTokenExpiredTime'])) {
            $res2 = (new ImmService())::main2($arr);
            if ($res2) {
                $arr['accessToken'] = $res2->body->accessToken;
                $arr['refreshToken'] = $res2->body->refreshToken;
                $arr['refreshTokenExpiredTime'] = $res2->body->refreshTokenExpiredTime;
                $arr['accessTokenExpiredTime'] = $res2->body->accessTokenExpiredTime;
                $arr['requestId'] = $res2->body->requestId;
                Cache::set($name,$arr,86400);
                $this->res_data['Msg']='OK';
                $this->res_data['Code']=0;
                $this->res_data['Data']=$arr;
            }
        }
        $res = (new ImmService())::main($name);
        $arr['previewURL'] = $res->body->previewURL;
        $arr['refreshToken'] = $res->body->refreshToken;
        $arr['requestId'] = $res->body->requestId;
        $arr['accessToken'] = $res->body->accessToken;
        $arr['refreshTokenExpiredTime'] = $res->body->refreshTokenExpiredTime;
        $arr['accessTokenExpiredTime'] = $res->body->accessTokenExpiredTime;
        // 缓存
        Cache::set($name,$arr,86400);
        $this->res_data['Msg']='OK';
        $this->res_data['Code']=0;
        $this->res_data['Data']=$arr;
        return $this->print_result($this->res_data);
    }


    public function forceDownload() { 
        $name = $_GET['name'];
        $arr = explode('/', $name);
        $filename = end($arr);
        $path = 'cljy/'.$filename;
        $service = new OssService();
        $res = $service->download_file($path);
        // http headers 
        header('Content-Type: application-x/force-download'); 
        header('Content-Disposition: attachment; filename="'.$filename.'"'); 
        // header('Content-length: ' . filesize($filename)); 
     
        // for IE6 
        if (false === strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6')) { 
            header('Cache-Control: no-cache, must-revalidate'); 
        } 
        header('Pragma: no-cache'); 
        return $res;
    } 
   
}
